package com.eshop.web.controllers.common;

import httl.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eshop.common.util.io.SmsTool;
import com.eshop.common.util.io.TransferHttpClient;
import com.eshop.common.util.security.MD5;
import com.eshop.frameworks.core.controller.BaseController;

@Controller
public class SmsController extends BaseController {

	@RequestMapping(value = "/sms/PassPortCode")
	@ResponseBody
	public String PassPortCode(String mobile, @RequestParam(required=false) String suleback, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", false);
		modelMap.put("message", "发送失败");
		if (StringUtils.isNotBlank(mobile)) {
			// 调取发送短信接口
			String[] content = SmsTool.getActiveUrlByMobile();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("mobiles", mobile);
			jsonObject.put("content", content[1]);
			TransferHttpClient.sendHttpPostJson(SmsTool.getSendSmsUrl(), jsonObject);
			this.setSessionAttribute(request, response, mobile + "_code",content[0]);
			modelMap.put("success", true);
			modelMap.put("message", "发送成功");
			modelMap.put("keyMobile", MD5.getMD5(mobile));
		}
		if(suleback==null){
			return  JSONObject.toJSONString(modelMap);
		}else{
			return suleback + "(" + JSONObject.toJSONString(modelMap) + ")";
		}
	}
}
